#!/usr/bin/python

import sys
import time
import signal
import argparse
from led import led
import myOut as msg

done = False
   
def signal_handler(signal, frame):
    print( 'unsafeLog: You pressed Ctrl+C!')
    global done
    done = True

def unsafeLog(sleepSecs):
    import platform
    from myLogger   import myLogger

    if platform.system() == 'Windows':
        from Adafruit_BMP085_sim import BMP085
    else:
        from Adafruit_BMP085 import BMP085
    # Initialise the BMP085 and use STANDARD mode (default value)
    # bmp = BMP085(0x77, debug=True)
        
    bmp085_addr = 0x77
    bmp = BMP085(bmp085_addr)
    # To specify a different operating mode, uncomment one of the following:
    # bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
    # bmp = BMP085(0x77, 1)  # STANDARD Mode
    # bmp = BMP085(0x77, 2)  # HIRES Mode
    # bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode

    tName = 'temp1'
    tFile = '/tmp/temp1'
    pName = 'pressure1'
    pFile = '/tmp/pressure1'
    aName = 'altitude1'
    aFile = '/tmp/altitude1'
    print( 'initializing loggers')
    tLogger = myLogger(tName, tFile)
    pLogger = myLogger(pName, pFile)
    aLogger = myLogger(aName, aFile)
    redLedPin = 17
    redLed = led(redLedPin)
    
    print(  'taking data')
    #while(not done):
    p=bmp.readPressure()
    a=bmp.readAltitude()
    t=bmp.readTemperature()
    
    redLed.on()
    pLogger.log(p)
    aLogger.log(a)
    tLogger.log(t)
    redLed.off()
        #time.sleep(sleepSecs)
    print 'graceful exit from unsafeLog'
        
if __name__ == "__main__":

    parser = argparse.ArgumentParser(description='test the unsafe logger.')
    parser.add_argument('--time', dest='sleepSecs', type=int,  default=5*60,
                       help='time to delay between readings (secs), default = 5 min')
        
    args = parser.parse_args()
    print 'arg is %d' % args.sleepSecs
    
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    unsafeLog(args.sleepSecs)

